import {getStorage} from './storage'
   // 获取当前坐标
   async function fnGps() {
      var _coordinate = ''
      await wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          var longitude = res.longitude//经度
          var latitude = res.latitude//维度
          _coordinate = longitude + ',' + latitude
          wx.setStorageSync('lnglat',_coordinate)
        },
        fail(err){
          wx.showToast({
            title:'请先授权小程序获取用户位置',
          });
          wx.setStorageSync('lnglat','116.3683244,39.915085')
        }
      })
      return _coordinate
    }
    // 获取UserInfo 信息
    async function fnGetUserInfo() {
      let _userInfo = await getStorage('userInfo')
      return _userInfo
    }
    function formatTime (time) {
      if (time) {
        let date = new Date((time.toString()).replace(/\-/g, "/"))
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h
        let minute = date.getMinutes()
        let second = date.getSeconds()
        minute = minute < 10 ? ('0' + minute) : minute
        second = second < 10 ? ('0' + second) : second
        return y + "-" + m + "-" + d
      }
    }

    /**
     * 将数值四舍五入(保留2位小数)后格式化成金额形式
     *
     * @param num 数值(Number或者String)
     * @return 金额格式的字符串,如'1,234,567.45'
     * @type String
     */
    function formatCurrency(num) {
      var num = num.toString().replace(/\$|\,/g, "");
      if (isNaN(num)) num = "0";
      var sign = num == (num = Math.abs(num));
      num = Math.floor(num * 100 + 0.50000000001);
      var cents = num % 100;
      num = Math.floor(num / 100).toString();
      if (cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num =
          num.substring(0, num.length - (4 * i + 3)) +
          "," +
          num.substring(num.length - (4 * i + 3));
      return (sign ? "" : "-") + num + "." + cents;
    }

    export default {
      fnGps,
      fnGetUserInfo,
      formatTime,
      formatCurrency
    }
